/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: ListTest
 * Author:   AEO WUGUOZHI
 * Date:     2020/7/20 22:01
 * Description: 学生可选课程表
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈学生可选课程表〉
 *
 * @author AEO WUGUOZHI
 * @create 2020/7/20
 * @since 1.0.0
 */
public class ListTest {
    public List coursesToSelect;

    public ListTest(){

        this.coursesToSelect = new ArrayList();
    }

    public void testAdd(){
        Course cr1 = new Course("1","数据结构");
        coursesToSelect.add(cr1);
        Course temp = (Course)coursesToSelect.get(0);
        System.out.println("课程id：" + temp.getId() + "  课程名称：" + temp.getName());

        Course cr2 = new Course("2","C语言");
        coursesToSelect.add(0,cr2);
        temp = (Course)coursesToSelect.get(0);
        System.out.println("课程id：" + temp.getId() + "  课程名称：" + temp.getName());

        coursesToSelect.add(cr1);
        Course temp0 = (Course)coursesToSelect.get(2);
        System.out.println("课程id：" + temp0.getId() + "  课程名称：" + temp0.getName());

//        Course cr3 = new Course("3","设计模式");           //抛出异常，数组小标越界
//        coursesToSelect.add(3,cr3);

        Course[] course = {new Course("3","离散数学"),new Course("4","汇编语言")};
        coursesToSelect.addAll(Arrays.asList(course));

        Course temp3 = (Course)coursesToSelect.get(3);
        Course temp4 = (Course)coursesToSelect.get(4);
        System.out.println("添加了两门课程" + temp3.getId() + ":" + temp3.getName() + "\n" + temp4.getId() + ":" + temp4.getName());

        Course[] course2 = {new Course("5","高等数学"),new Course("6","大学英语")};
        coursesToSelect.addAll(2,Arrays.asList(course2));
        Course temp5 = (Course)coursesToSelect.get(2);
        Course temp6 = (Course)coursesToSelect.get(3);
        System.out.println("添加了两门课程" + temp5.getId() + ":" + temp5.getName() + "\n" + temp6.getId() + ":" + temp6.getName());
    }

/*
取得List中的元素的方法
 * */

    public void testGet(){
        int size = coursesToSelect.size();
        System.out.println("有如下课程待选：");
        for(int i = 0;i < size;i++){
            Course cr = (Course)coursesToSelect.get(i);
            System.out.println("课程："+cr.getId() + "：" + cr.getName());
        }
    }

/*
* 通过迭代器来遍历List,迭代器只是用来遍历集合中的元素
* */

    public void testIterator(){
        Iterator it = coursesToSelect.iterator();
        System.out.println("有如下课程待选(通过迭代器访问)");
        while(it.hasNext()){
            Course cr = (Course)it.next();
            System.out.println("课程："+cr.getId() + "：" + cr.getName());
        }
    }

/*
* 通过foreach方法访问集合元素
* */

    public void testForEach(){
        System.out.println("有如下课程待选(通过foreach()访问)");
        for(Object obj:coursesToSelect){
            Course cr = (Course)obj;
            System.out.println("课程："+cr.getId() + "：" + cr.getName());
        }
    }

/*
* 修改List中的元素
* */
    public void testModify(){
        coursesToSelect.set(4,new Course("7","毛概"));
    }

/*
* 删除List中的元素
* */

    public void testRemove(){
//        Course cr = (Course)coursesToSelect.get(4);
//        System.out.println("我是课程" + cr.getId() + ":" + cr.getName() + ",我即将被删除");
//        System.out.println("即将删除位置4上的元素");
//        coursesToSelect.remove(4);

        System.out.println("即将删除位置4 and 5上的元素");
        Course[] courses = {(Course)coursesToSelect.get(4),(Course)coursesToSelect.get(5)};
        coursesToSelect.removeAll(Arrays.asList(courses));
        System.out.println("成功删除课程！");
        testForEach();
    }

/*
* 往List中添加一些奇怪的东西
* */

    public void testType(){
        System.out.println("能否往List中添加一些奇怪的东西");
//        coursesToSelect.add("我不是课程类，我是一个字符串");
    }

/**
 * 泛型不能使用基本类型，使用对应的包装类
 * */

    public void testBasicType(){
        List<Integer>list = new ArrayList<>();
        list.add(1);
        System.out.println("基本类型必须使用包装类作为泛型" + list.get(0));
    }

    public static void main(String[] args) {
        ListTest lt = new ListTest();
        lt.testAdd();
        lt.testType();
        lt.testForEach();

        ListTest a = new ListTest();
        a.testBasicType();

        System.out.println("-----------------------");
        List aa = new ArrayList();

        aa.add("0.2");
        aa.add(new Course("1","a"));
        for (Object k:aa) {
            System.out.println(k.toString());
        }

//        lt.testGet();
//        lt.testIterator();
//        lt.testForEach();
//        lt.testModify();
//        lt.testForEach();
//        lt.testRemove();
    }



}